package com.zjpavt.client.artnet.effect;

import com.zjpavt.client.artnet.device.StripLight;

/**
 * 方波
 * 速度为负数的时候向下 速度为正数的时候向上
 * @author zyc
 */
public class SquareWaveEffect implements IEffect {
    private transient  StripLight stripLight;
    private byte[] backGround;
    private byte[] runColor;
    private double variate;
    private double speed = 1;
    private int period;
//  相位
    private int phase;

    public SquareWaveEffect(StripLight stripLight, byte[] backGround, byte[] runColor, int period, int phase) {
        this.stripLight = stripLight;
        this.backGround = backGround;
        this.runColor = runColor;
        this.period = period;
        this.phase = phase;
    }

    public SquareWaveEffect(StripLight stripLight, byte[] backGround, byte[] runColor, int period, int phase,double speed) {
        this.stripLight = stripLight;
        this.backGround = backGround;
        this.runColor = runColor;
        this.period = period;
        this.phase = phase;
        this.speed = speed;
    }

    @Override
    public void show() {
        int channelNum = stripLight.getSingleLightChannelSize();
        int lightNum = stripLight.getLightNum();
        if (channelNum != backGround.length || channelNum != runColor.length) {
            return;
        }
        for (int index = 0; index < stripLight.getLightNum(); index++) {
            if (1  <  Math.abs((index + phase +variate) / period % 2)) {
                stripLight.setLightColor(index, runColor);
            } else {
                stripLight.setLightColor(index, backGround);
            }
        }
        variate+= speed;
    }

    @Override
    public void setOwner(StripLight stripLight) {
        this.stripLight = stripLight;
    }
}
